<?php
 include_once('config/config.php');
 // 获取id
 if(!$_GET){
 	ShowMsg('请选择编辑的数据','Adminlist.php');
 	exit;
 }
 // id
 $id = $_GET['id'];
 // 查询单条数据
 $sql = "SELECT * FROM `pre_admin` WHERE `id` = '$id'";
 
 $admin = find($sql);

 // 判断有没有表单提交过来
 if($_POST){
	// 接收全部参数
	    $params = $_POST;
	
	    // 封装更新数组
	    $data = [
	      'username' => $params['username']
	    ];
		// 是否输出原密码
	    if($params['pass'])
	    {
	      if(empty($params['password']))
	      {
	        ShowMsg('新密码不能为空');
	        exit;
	      }
	
	      if(empty($params['passwords']))
	      {
	        ShowMsg('新密码不能为空');
	        exit;
	      }
	
	      // 先拿到表单的原密码跟当前的数据里的密码盐进行md5加密
	      $pass = md5($params['pass'].$admin['salt']);
	
	      // 加密后的密码判断是否跟数据库的密码一致
	      if($pass != $admin['password'])
	      {
	        ShowMsg('密码错误');
	        exit;
	      }
	
	      if($params['password'] != $params['passwords'])
	      {
	        ShowMsg('密码与确认密码不一致');
	        exit;
	      }
	
	      $salt = build_ranstr();
	
	      $data['salt'] = $salt;
	      $data['password'] = md5($params['password'].$salt);
	
	    }
	    // 判断是否有文件上传且大小
	    if($_FILES['avatar'] && $_FILES['avatar']['size'] > 0)
	    {
	      // 调用上传函数
	      $avatar = upload('avatar');
	
	      if($avatar['code'] == 0)
	      {
	        ShowMsg($avatar['msg']);
	        exit;
	      }else{
	        $data['avatar'] = $avatar['data'];
	      }
	    }
	
	    $result = update('admin',$data,"`id` = '$id'");
	
	    if($result)
	    {
	      if(isset($data['avatar']) && $_FILES['avatar']['size'] > 0)
	      {
	        @is_file($admin['avatar']) && @unlink($admin['avatar']);
	      }
	
	      ShowMsg('更新成功','AdminList.php');
	      exit;
	    }else{
	      
	      @is_file($data['avatar']) && @unlink($data['avatar']);
	
	      ShowMsg('更新失败');
	      exit;
	    }
 }
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include_once('common/meta.php'); ?>
	</head>
	<style>
		.avatar{
			width: 200px;
			height: 200px;
			display: inline-block;
			user-select: none;
			cursor: pointer;
		}
		.avatar img{
			width: 200px;
			height: 200px;
		}
		
	</style>
	<body>
		<!--<![endif]-->

		<?php include_once('common/header.php'); ?>

		<?php include_once('common/sidebar.php'); ?>

		<div class="content">
			<div class="header">
				<h1 class="page-title">编辑管理员</h1>
			</div>
			<ul class="breadcrumb">
				<li><a href="index.php">Home</a> <span class="divider">/</span></li>
				<li class="active">Index</li>
			</ul>
			<div class="container-fluid">
				<div class="row-fluid">

					<div class="btn-toolbar">
						<button class="btn btn-primary" onClick="location='Adminlist.php'"><i class="icon-list"></i>管理员列表</button>
						<div class="btn-group">
						</div>
					</div>

					<div class="well">
						<div id="myTabContent" class="tab-content">
							<div class="tab-pane active in" id="home">
								<form method="post" enctype="multipart/form-data">
									<!-- 用户名 -->
									<label>用户名</label>
									<input type="text" name="username" class="input-xxlarge" placeholder="请输入用户名" value="<?php echo $admin['username']?>" required />
									<!-- 密码 -->
									<label>原密码</label>
									<input type="text" name="pass" class="input-xxlarge" placeholder="请输入密码" required />
									<!-- 密码 -->
									<label>新密码</label>
									<input type="text" name="password" class="input-xxlarge" placeholder="请输入密码" required />
									<!-- 确认密码 -->
									<label>确认密码</label>
									<input type="text" name="passwords" class="input-xxlarge" placeholder="请确认密码" required />
									<!-- 头像 -->
									<label>头像</label>
									<input type="file" name="avatar" id="avatar" hidden="hidden"/>
									<label for="avatar" class="avatar">
										<img src="<?php echo $admin['avatar']; ?>" id="img" />
									</label>
									<br />
									<button type="button" class="btn btn-primary">选择文件</button>
									<!-- 提交 -->
									<button type="submit" class="btn btn-primary">提交</button>
								</form>
							</div>
						</div>
					</div>
					<?php include_once('common/footer.php'); ?>

				</div>
			</div>
		</div>

		<?php include_once('common/script.php'); ?>

	</body>
</html>
<script>
  // 图片预览函数
  function GetObjectURL(file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }

  $('#avatar').change(function(){

    // 获取选中的文件
    let file = $(this)[0].files[0] ? $(this)[0].files[0] : {};
    
    // 获取文件地址
    let url = GetObjectURL(file)

    // 赋值给img
    $('#img').attr('src',url)

  })

</script>